<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="content.name"></el-input>
      <el-button @click="remove">清除</el-button>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" :index="indexMethod" label="任务编号" width="80" prop="index">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="upLoad(row.id)">编辑</el-button>
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="del(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :current-page="params._page"
        :page-size="params._limit"
        @current-change="pageChange"
      >
      </el-pagination>
      <el-button size="small" plain @click="openDialog">+ 新增</el-button>
    </div>
    <el-dialog title="提示" :visible="dialogVisible" width="30%" @close="closeDialog">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="form.des"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { v4 as uuidv4 } from 'uuid'
import {
  getPeopleList,
  delPeople,
  addPeople,
  getPeopleContent,
  uploadTask,
  searchName
} from '@/api/people.js'
export default {
  data() {
    return {
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      list: [],
      total: 0,
      dialogVisible: false,

      //要新增的数据
      form: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: { required: true, message: '请输入任务名称', trigger: 'blur' },
        des: { required: true, message: '请输入任务描述', trigger: 'blur' }
      },
      content: {
        name: ''
      }
    }
  },
  mounted() {
    this.uploadList()
  },
  methods: {
    async uploadList() {
      const res = await getPeopleList(this.params)
      this.list = res.data
      this.total = +res.headers[`x-total-count`]
    },

    //分页改变
    pageChange(page) {
      this.params._page = page
      this.uploadList()
    },

    //序号排列
    indexMethod(index) {
      return (this.params._page - 1) * this.params._limit + index + 1
    },

    //删除任务
    async del(id) {
      await delPeople(id)
      this.$message.success('删除任务成功')
      this.uploadList()
    },

    //打开新增对话框
    openDialog() {
      this.dialogVisible = true
    },

    //添加任务
    add() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        if (this.form.id) {
          console.log(this.form)
          await uploadTask(this.form)
          this.$message.success('编辑任务成功')
        } else {
          this.form.id = uuidv4()
          await addPeople(this.form)
          this.$message.success('新增任务成功')
        }
        this.closeDialog()
        this.uploadList()
      })
    },

    //关闭新增对话框
    closeDialog() {
      this.dialogVisible = false
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        des: '',
        id: ''
      }
    },

    //打开编辑框
    async upLoad(id) {
      this.dialogVisible = true
      const res = await getPeopleContent(id)
      this.form = res.data
    },

    //清除按钮
    remove() {
      this.content.name = ''
      this.uploadList()
    },

    //搜索
    async search() {
      const res = await searchName(this.content)
      this.list = res.data
      this.total = 1
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
